/**
 * @Author ZhaoLei
 * @FileName memberInfo.js
 * @Description  hft 会员信息
 * @Date 2022-10-28 17:43
 * @Copyright By hftsoft
 */
import HftApi from "@/repo/api/HftApi";

const state = {
  // (int32)门店是否开通 0=未开通 1=已开通
  detpIsOpen: 0,
  // (int32)是否需要进行老数据兼容 0=不需要 1=需要
  ordCompatible: 0,
  // (int32)用户是否开通 0=未开通 1=已开通
  userIsOpen: 0,
  // 用户是否是店长 0=不是 1=是
  isManager: 0,
  // 企业大学是否开通 0=不是 1=是
  universityIsOpen: 0,
  zhiYeSign: null
};

const mutations = {
  SET_DEPT_IS_OPEN: (state, data) => state.detpIsOpen = data,
  SET_ORD_COMPATIBLE: (state, data) => state.ordCompatible = data,
  SET_USER_IS_OPEN: (state, data) => state.userIsOpen = data,
  SET_IS_MANAGER: (state, data) => state.isManager = data,
  SET_ZHIYE_SIGN: (state, data) => state.zhiYeSign = data,
  SET_UNIVERSITY_IS_OPEN: (state, data) => state.universityIsOpen = data
};

const actions = {
  getMemberInfo: async ({ commit }) => {
    if (!window.hasOwnProperty('zshft')) return;
    let archiveId = window.zshft.getArchiveInfo('archiveId');
    let { detpIsOpen, ordCompatible, userIsOpen, isManager, universityIsOpen } = await HftApi.getMemberInfo(archiveId);
    commit('SET_DEPT_IS_OPEN', detpIsOpen);
    commit('SET_ORD_COMPATIBLE', ordCompatible);
    commit('SET_USER_IS_OPEN', userIsOpen);
    commit('SET_IS_MANAGER', isManager);
    commit('SET_UNIVERSITY_IS_OPEN', universityIsOpen);
    const { sign = '' } = await HftApi.getZhiYeSign(archiveId);
    commit('SET_ZHIYE_SIGN', sign);
  }
};

const getters = {
  detpIsOpen: (state) => state.detpIsOpen || 0,
  ordCompatible: (state) => state.ordCompatible || 0,
  userIsOpen: (state) => state.userIsOpen || 0,
  isManager: (state) => state.isManager || 0,
  sign: (state) => state.zhiYeSign || null,
  isOpen: (state) => state.detpIsOpen && state.userIsOpen,
  universityIsOpen: state => state.universityIsOpen || 0
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
